﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using hlavny.modul1;

namespace hlavny.modul2
{
    [Serializable]
    public class CSysColumns
    {
        private List<CAttribute> columns = null;
        private int lastId = 0;

        #region CSysColumns properties

        public List<CAttribute> Columns
        {
            get { return columns; }
            set { columns = value; }
        }
        public int LastId
        {
            get { return lastId; }
            set { lastId = value; }
        }

        #endregion

        #region CSysColumns Constructors

        /// <summary>
        /// CSysColumns constructor
        /// </summary>
        public CSysColumns()
        {
            columns = new List<CAttribute>();
        }

        #endregion

        #region Help functions

        /// <summary>
        /// Fuction search for attribute id and return index of attribute in sysColumns
        /// </summary>
        /// <param name="id">id of the attribute</param>
        /// <returns>index of attribute in sysColumns, if not found returns negative number</returns>
        public int GetAttributeIndex(int id)
        {
            return columns.BinarySearch(new CAttribute(id));
        }

        /// <summary>
        /// Function set if attribute could by null or not
        /// </summary>
        /// <param name="attribute">Reference to the attribute</param>
        /// <param name="isNull">True if could by null, otherwise false</param>
        /// <returns>True if the setting nullability is done, otherwise false</returns>
        public bool SetNullability(CAttribute attribute, bool isNull)
        {
            attribute.IsNull = isNull ? true : false;
            Save();
            return true;
        }

        /// <summary>
        /// Function get id of column table 
        /// </summary>
        /// <param name="id">column id</param>
        /// <returns>Id of column table, </returns>
        public int GetColumnTable(int id)
        {
            int index = GetAttributeIndex(id);
            if (index < 0)
            {
                return -1;
            }
            else
            {
                return columns[index].Table;
            }
        }

        #endregion

        #region SysColumns main functions

        /// <summary>
        /// Function create new attribute
        /// </summary>
        /// <param name="name">Name of the attribute</param>
        /// <param name="type">Type of the attribute</param>
        /// <param name="length">Length of the attribute</param>
        /// <param name="isNull">Could or couldn't by null</param>
        /// <returns>Returns new object CAttribute</returns>
        public CAttribute CreateAtr(string name, CTypes.EnumTypes type, int length, bool isNull, int table)
        {
            lastId++;
            CAttribute attribute = new CAttribute(lastId, table, name, type, length, isNull);
            columns.Add(attribute);
            columns.Sort();
            Save();
            return attribute;
        }

        /// <summary>
        /// Function drop column
        /// </summary>
        /// <param name="attribute">Reference to the column to drop</param>
        /// <returns>True if drop column, otherwise false</returns>
        public bool DropAtr(CAttribute attribute)
        {
            int index = GetAttributeIndex(attribute.Id);
            if (index < 0)
            {
                return false;
            }
            else
            {
                columns.RemoveAt(index);
                columns.Sort();
                Save();
            }
            return true;
        }

        /// <summary>
        /// Function set type for attribute
        /// </summary>
        /// <param name="attribute">Reference to attribute</param>
        /// <param name="type">Type of CAttribute.EnumAttributeTypes to set for attribute</param>
        /// <returns>True if setting type is done, otherwise false</returns>
        public bool SetType(CAttribute attribute, CTypes.EnumTypes type)
        {
            attribute.Type = type;
            Save();
            return true;
        }

        /// <summary>
        /// Function set length for attribute
        /// </summary>
        /// <param name="id">Reference to attribute</param>
        /// <param name="length">Length to set up to attribute</param>
        /// <returns>True if setting length is done, otherwise false</returns>
        public bool SetLength(CAttribute attribute, int length)
        {
            attribute.Length = length;
            Save();
            return true;
        }

        /// <summary>
        /// Function set that attribute couldn't by null 
        /// </summary>
        /// <param name="attribute">Reference to the attribute</param>
        /// <returns>True if the setting is done, otherwise false</returns>
        public bool DropNull(CAttribute attribute)
        {
            return SetNullability(attribute, false);
        }

        /// <summary>
        /// Function set that attribute could by null 
        /// </summary>
        /// <param name="attribute">Reference to the attribute</param>
        /// <returns>True if the setting is done, otherwise false</returns>
        public bool SetNull(CAttribute attribute)
        {
            return SetNullability(attribute, true);
        }

        #endregion

        #region Save function

        /// <summary>
        /// Function serialize all table sysColumns to the file
        /// </summary>
        public void Save()
        {
            CFunctions.StoreSysColumns(this);
        }

        #endregion



        
    }
}
